<%@ page language="java" import="java.util.*" pageEncoding="utf8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="com.niit.bc.bean.*" %>
<%@ page import="com.niit.bc.util.web.*" %>

<script type="text/javascript">
    var xmlHttpReq = null;
    function addCart() {
        var bookId = document.getElementById("bookId").value;//图书Id号
        var bookCount = parseInt(document.getElementById("bookCount").value);//用户填写的图书购买数量
        var bookMaxCount = parseInt(document.getElementById("bookMaxCount").value);//图书的库存量
        var op = 'add';
//alert(bookId);
//alert(bookCount);
//alert(bookMaxCount);
        if (bookMaxCount <= 0) {
            alert("库存有" + bookMaxCount + "件,无法购买图书!");
            return;
        } else if (bookCount > bookMaxCount) {//判断用户的购买量是否大于图书的库存量，如果大于将值默认设置为库存量的值
            alert("库存只有" + bookMaxCount + "件,填写的购买量不能超过" + bookMaxCount + "件");
            document.getElementById("bookCount").value = bookMaxCount;
            return;
        } else if (bookCount <= 0) {
            alert("请填写购买数量，数量不能为0");
            return;
        } else {
            if (window.XMLHttpRequest) {
                xmlHttpReq = new XMLHttpRequest();
            }
            if (window.ActiveXObject) {
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            }
//alert('CartBook?bookId='+bookId+'&bookCount='+bookCount+'&op='+op);
            xmlHttpReq.open('GET', 'CartBook?bookId=' + bookId + '&bookCount=' + bookCount + '&op=' + op, true);//提交方式、地址和参数、是否异步提交
            xmlHttpReq.onreadystatechange = callback; //设置回调函数
            xmlHttpReq.setRequestHeader("if-Modified-since", "0");//设置不使用浏览器缓存
            xmlHttpReq.send(null);//发送请求
        }

    }

    function callback() {
        if (4 == xmlHttpReq.readyState && 200 == xmlHttpReq.status) {
            var res = xmlHttpReq.responseXML.getElementsByTagName("cart");
            var spanValue = 0;
            for (var i = 0; i < res.length; i++) {
                var nodeName = res[i].getElementsByTagName("bookCount")[0];
                //alert(nodeName.firstChild.data);
                //divShow += "<div>+"nodeName.firstChild.data"+</div><br/>";

                spanValue = nodeName.firstChild.data;
            }
//alert("zhi:"+spanValue);
            var spanMyCart = document.getElementById("myCart");
            spanMyCart.innerHTML = spanValue;
            alert("购物车添加成功");
        }

    }

    function goBuy() {

//var goBuy = document.getElementById("goBuy");
        var userId = document.getElementById("userId").value;
        var bookCount = parseInt(document.getElementById("bookCount").value);//用户填写的图书购买数量
        var bookMaxCount = parseInt(document.getElementById("bookMaxCount").value);//图书的库存量
        var bookId = document.getElementById("bookId").value;//图书Id号
        if (userId == 0) {
            alert("友情提示\n\n请先登录，再购买图书!");
            return;
        } else if (bookMaxCount <= 0) {
            alert("库存有" + bookMaxCount + "件,无法购买图书!");
            return;
        } else if (bookCount > bookMaxCount) {//判断用户的购买量是否大于图书的库存量，如果大于将值默认设置为库存量的值
            alert("库存只有" + bookMaxCount + "件,填写的购买量不能超过" + bookMaxCount + "件");
            document.getElementById("bookCount").value = bookMaxCount;
            return;
        } else if (bookCount <= 0) {
            alert("请填写购买数量，数量不能为0");
            return;
        }

        window.location.href = "CreateOrder?bookId=" + bookId + "&bookCount=" + bookCount;

    }
</script>

<!--图书基本信息-->
<%
    List <BookBean> bookList = null;
    BookBean book = null;
    if (request.getAttribute("bookList") != null) {
        bookList = (List <BookBean>) request.getAttribute("bookList");
    }
    for (int i = 0; i < bookList.size(); i++) {
        book = bookList.get(i);
%>
<div class="biaoti"><%=book.getBookName() %>
</div>
<hr size=2 color="#e8e8e8"/>
<div class="productbox">
    <div class="productl">
        <IMG class="ProductImg" id="PreSmallPic"
             style="margin:10px 50px 10px 50px;width:220px; height:280px;border-width:0;"
             src="<%=book.getBookPhoto() %>" alt="<%=book.getBookName() %>">
    </div>

    <div class="productr">
        <ul>
            <li>图书编号： <span><%=book.getBookId() %></span></li>
            <li>图书ISBN：<span><%=book.getBookIsbn() %></span></li>
            <li>作者： <span><%=book.getBookAuthor()%></span></li>
            <li>定价： <span class="price"><%=book.getBookPrice() %></span> 元</li>
            <li>出版社： <span> <%=book.getBookPub() %></span>
            <li>版次：[第<strong><font color="#ff6600"><%=book.getBookPubTime() %>
            </font></strong>版]
            </li>
            <li>上架时间： <span><%=book.getBookStorageTime() %></span></li>
            <li>已 销 售： <span class="sales"><%=book.getBookSale() %></span> 件</li>
            <li>浏 览 量： 已有<span class="hits"><%=book.getBookHits() %></span>人关注</li>
        </ul>
        <ul>
            <div class="carbox">
                <!-- 通过隐藏域传值到购物车或订单页面 -->
                <form name="cartform" id="cartform" method="post" action="  ">
                    <input type="hidden" name="action" value="Add">
                    <div>我要买：
                        <input type="text" onkeyup="this.value=this.value.replace(/\D/,'');" name="bookCount"
                               id="bookCount" size="4" value="1" style="text-align:center"> 件
                        <label style="color:#999">
                            (库存<label style="color:#ff6600;font-weight: bolder;"><%=book.getBookNum() %>
                        </label>件)
                        </label>
                    </div>
                    <div>
                        <c:if test="${niit_bc_user != null }">
                            <input type="hidden" name="userId" id="userId" value="${niit_bc_user.userId }"/>
                        </c:if>
                        <c:if test="${niit_bc_user == null }">
                            <input type="hidden" name="userId" id="userId" value="0"/>
                        </c:if>
                        <input type="hidden" name="bookId" id="bookId" value="<%=book.getBookId()%>"/>
                        <input type="hidden" name="bookMaxCount" id="bookMaxCount" value="<%=book.getBookNum() %>"/>
                        <input type="button" id="buybtn" onclick="goBuy()"/>
                        <input type="button" id="carbtn" onclick="addCart()"/>
                    </div>
                </form>
            </div>
        </ul>
    </div>
</div>
<!--图书基本信息-->
<div class="blank10 clear"></div>


<!--图书详情信息-->
<div class="bookborder">
    <div class="bookdetailtitle">图书详情</div>
    <div class="bookdetailbox">
        <%=book.getBookDescribe() %>
    </div>
</div>
<%} %>
<!--图书详情信息-->
